package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 订单任务类
 *    每隔1分钟去获取所有的未支付的订单
 *    如果当前时间 - 15分钟  > 下单时间  => 超时,订单状态 / 取消原因 /取消时间
 */
@Component
public class OrderTask {
    @Autowired
    OrderMapper orderMapper;
    @Scheduled(cron = "0 * * * * ?")
    public void cancelTask(){
        //1.获取当前时间  - 15分钟
        LocalDateTime time = LocalDateTime.now().minusMinutes(15l);
        //2.获取所有用户的超时的订单
        List<Orders> orderList = orderMapper.findOvertimeOrders(time);
        //3.改状态
        orderList.forEach(
                orders -> {
                    //修改状态
                    orders.setStatus(Orders.CANCELLED);
                    orders.setCancelTime(LocalDateTime.now());
                    orders.setCancelReason("订单超时,自动取消");
                    orderMapper.update(orders);
                }
        );

    }
}
